package com.gec.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.system.service.SysCategoryService;
import com.gec.system.system.SysCategory;
import com.gec.system.system.SysMenu;
import com.gec.system.util.Result;
import com.gec.system.vo.SysQueryCategoryVo;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api
@RequestMapping("/admin/system/category")
public class SysCategoryController {
    private static final Logger log = LoggerFactory.getLogger(SysCategoryController.class);
    @Autowired
    private SysCategoryService categoryService;
    @GetMapping("findAll")
    public Result findAll(){
        List<SysCategory> list = categoryService.list();
        return Result.ok(list);
    }
    @GetMapping("find/{id}")
    public Result findCategoryById(@PathVariable Long id){
        SysCategory category = categoryService.getById(id);
        return Result.ok(category);
    }

    @GetMapping("{page}/{limit}")
    public Result findAllCategory(@PathVariable("page") Long pageIndex, @PathVariable Long limit , SysQueryCategoryVo vo){
        Page<SysCategory> page = new Page<>(pageIndex, limit);
        IPage<SysCategory> sysCategoryIPage = categoryService.selectPage(page, vo);
        return Result.ok(sysCategoryIPage);
    }
    @DeleteMapping("/delete/{id}")
    public Result removeById(@PathVariable Long id){
        boolean remove = categoryService.removeById(id);
        return remove ? Result.ok() : Result.fail();
    }
    @DeleteMapping("/delete")
    public Result removeByIds(@RequestBody List<Long> ids){
        boolean removeByIds = categoryService.removeByIds(ids);
        return removeByIds?Result.ok():Result.fail();

    }
    @PostMapping("save")
    public Result addCategory(@RequestBody SysCategory sysCategory){
        boolean save = categoryService.save(sysCategory);
        return save ?Result.ok():Result.fail();
    }
    @PostMapping("update")
    public Result updateCategory(@RequestBody SysCategory sysCategory){
        boolean update = categoryService.updateById(sysCategory);
        return update ?Result.ok():Result.fail();
    }



}
